home *** CD-ROM | disk | FTP | other *** search
- Path: news.wright.edu!emoyer
- From: emoyer@cs.wright.edu (Eric Moyer)
- Newsgroups: comp.lang.c++
- Subject: Re: Why use private class members instead of protected?
- Date: 16 Jan 1996 14:34:16 GMT
- Organization: Wright State University, Dayton, OH 45435
- Message-ID: <4dgd18$as0@mercury.wright.edu>
- References: <30F4AB49.6ABB@sierra.net> <30F50874.15FB7483@intellektik.informatik.th-darmstadt.de>
- NNTP-Posting-Host: gamma.cs.wright.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Enno Sandner (enno@intellektik.informatik.th-darmstadt.de) wrote:
- >TGColwell wrote:
- >>
- >> I'm relatively new to c++. I have one quick question: If child
- >> classes can only access protected members of the parent class,
- >> why make any members of any class private? Wouldn't it be
- >> better to make members of the parent class protected so that the
- >> class is alway "inheritance ready"?
- >No,
- >it's quite often a better approach to make these data-members private and
- >provide the suitable accessor functions. In this way you can change the
- >representation of the data-members without the problem that modifications
- >will 'propagate' downwards the class-hierarchy.
-
- I agree. However, there are still instances when one would make a data
- member protected. In particular, very basic (as in linked list) classes
- with a stable representation should have protected data members. Why? For
- two reasons.
-
- 1. Being basic classes, they (and their derived classes) are likely to
- be heavily used, especially in inner loops. Even minor
- optimizations, such as eliminating copying for an accessor function
- would have benefits throughout your source code.
-
- 2. Since they have a stable, efficient representation, and have been
- studied for years, the representation is unlikely to change and
- cause problems throughout the class hierarchy.
-
- Also, I like to declare functions protected rather than private. In
- particular, I declare many accessor/mutator functions as follows:
-
- public:
- DATA get_Data() const;
- protected:
- //Returns FALSE on error
- //<BOOL and FALSE are macros allowing compiler independence>
- BOOL set_Data(DATA const &d);
-
- --Eric
- --
- -------------------------------------__|__-----------------------------------
- "A wise man once said nothing" | Emoyer@valhalla.cs.wright.edu
- ---------------------------------------| ------------------------------------
- GCS/M d(-+) p++@ c++++ l+ u++ e+(*) m++@ s+/ !n h--(*) f+ g+ w+++ t@ r+@ !y
-